Imports System.Threading

Module ThreadPoolDemo
    Sub DisplayA(ByVal state As Object)
        For I As Integer= 0 To 100
            Console.Write("A")
        Next

        Thread.Sleep(3000)

        CType(state, AutoResetEvent).Set()
    End Sub

    Sub DisplayB(ByVal state As Object)
        For I As Integer= 0 To 100
            Console.Write("B")
        Next

        Thread.Sleep(3000)

        CType(state, AutoResetEvent).Set()
    End Sub

    Sub DisplayC(ByVal state As Object)
        For I As Integer= 0 To 100
            Console.Write("C")
        Next

        Thread.Sleep(3000)

        CType(state, AutoResetEvent).Set()
    End Sub

    Sub Main()
        Dim AIsDone As New AutoResetEvent(False)
        Dim BIsDone As New AutoResetEvent(False)
        Dim CIsDone As New AutoResetEvent(False)

        ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf DisplayA), AIsDone)
        ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf DisplayB), BIsDone)
        ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf DisplayC), CIsDone)
        Dim ThreadCount, ThreadPorts As Integer

        ThreadPool.GetAvailableThreads(ThreadCount, ThreadPorts)
        Console.WriteLine("Thread pool size {0}", ThreadCount)
        Console.WriteLine()
        Console.WriteLine("Dang cho cac thread bat dong bo hoan tat.")

        AIsDone.WaitOne()
        Console.WriteLine()
        Console.WriteLine("A Xong")

        BIsDone.WaitOne()
        Console.WriteLine()
        Console.WriteLine("B Xong")

        CIsDone.WaitOne()
        Console.WriteLine()
        Console.WriteLine("C Xong")

        Console.ReadLine()  
    End Sub

End Module
